# -*- coding: utf-8 -*-
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect, HttpRequest
from urllib import urlencode
import functools


def loginzauth_required(func):
    def decorated(*args, **kwargs):
        request = kwargs.get('request')
        if not request:
            for request in args:
                if isinstance(request, HttpRequest):
                    break
        if getattr(request, 'loginzauth_user', None):
            return func(*args, **kwargs)
        else:
            return HttpResponseRedirect('%s?%s' % (reverse('login'),
                urlencode({'return_to': request.build_absolute_uri()})))
    return functools.update_wrapper(decorated, func)
